package com.op.teachingliteapp.fragment.dropdown;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.activity.LoginActivity;
import com.op.teachingliteapp.bean.UserBean;

public class DropDownAdapter extends BaseAdapter {

	private static final String TAG = DropDownAdapter.class.getSimpleName();
	private Context context;
	private ArrayList<UserBean> list;

	public DropDownAdapter(Context context, ArrayList<UserBean> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		Log.i(TAG, "" + list.get(position).getNumber());
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final String item = list.get(position).getNumber();
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_drop_down, null);
			viewHolder = new ViewHolder();
			viewHolder.itemTv = (TextView) convertView.findViewById(R.id.drop_down_list_item);
			viewHolder.itemImageButton = (ImageButton) convertView.findViewById(R.id.drop_down_list_item_delete);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// 将可以登录的帐号信息添加进去
		viewHolder.itemTv.setText(item);
		viewHolder.itemTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 将点击到的用户名加到输入框
				LoginActivity.userNumInput.setText(item);
				LoginActivity.popupWindow.dismiss();
				LoginActivity.dropDown.setBackgroundResource(R.drawable.login_drop_down_button);
			}
		});
		// 删除按钮点击事件
		viewHolder.itemImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		convertView.setTag(viewHolder);

		return convertView;
	}

	// 每个item的控件定义
	private static class ViewHolder {
		private ImageButton itemImageButton;
		private TextView itemTv;
	}

}
